<?php

namespace App\Http\Controllers\API\Activity;

use Illuminate\Http\Request;
use App\Http\Controllers\API\APIController;

use Validator;
use App\Events\NewNotification;
use App\Models\API\Notification;
use App\Models\API\Activity;
use App\Models\API\User;
use Auth;
use DB;

class ApplyController extends APIController
{
    public function index($id)
    {
        $users = \App\Models\API\ActivityApply::where('activity_id', $id)
            ->join('users', 'activity_applies.user_id', '=', 'users.id')
            ->select('activity_applies.*', 'users.user_name', 'users.user_img')
            ->get();
        return response()->json($this->setJson(true, $users), 200);
    }

    public function apply(Request $request, $id)
    {
    	$v = Validator::make($request->all(), [
            'user_id'   => 'required'
        ]);

        if ($v->fails())
        {
        	return response()->json($this->setJson(false, $v->errors()), 422);
        }

		if ($v->passes())
		{
            $applyUser = User::find($request->get('user_id'));
            $activity = Activity::find($id);
            
            if (\App\Models\API\ActivityJoin::isJoined($activity->id, $applyUser->id)) {
                return response()->json($this->setJson(true, 'is_joined'), 200);
            } else {
                $userInfo = $request->only('user_id');
                $res = $activity->apply($userInfo);
                // 申请后通知发布者
                $detail = [
                    'activity_id' => $activity->id,
                    'title'       => $activity->title,
                    'user_id'     => $applyUser->id,
                    'user_name'   => $applyUser->user_name,
                    'user_img'    => $applyUser->user_img,
                    'sign'        => '申请加入你的活动，是否同意？',
                    'is_joined'   => 0,
                ];

                $notifications = Notification::where([
                    ['user_id', $activity->user_id],
                    ['type', 'new_activity_apply'],
                    ['detail', json_encode($detail)],
                ])->first();

                if (count($notifications) === 0) {
                    $notification = new Notification;
                    $notification->user_id = $activity->user_id;
                    $notification->type = 'new_activity_apply';
                    $notification->detail = json_encode($detail);
                    $notification->status = 0;
                    $notification->save();
                    event(new NewNotification($notification));
                }

                return response()->json($this->setJson(true, $res), 200);
            }
		}
    }

    public function cancel(Request $request, $id)
    {
    	$v = Validator::make($request->all(), [
            'user_id'   => 'required'
        ]);

        if ($v->fails())
        {
        	return response()->json($this->setJson(false, $v->errors()), 422);
        }

		if ($v->passes())
		{
			$userInfo = $request->only('user_id');
			$res = Activity::find($id)->cancelApply($userInfo);
			return response()->json($this->setJson(true, $res), 200);
		}
    }

    private function setJson($status, $msg)
    {
        $res = [
            'status' => $status,
            'msg'    => $msg
        ];
        return $res;
    }
}
